package cn.itcast.controller.system;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.system.Dept;
import cn.itcast.domain.system.Role;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.DeptService;
import cn.itcast.service.system.RoleService;
import cn.itcast.service.system.UserService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/system/user")
public class UserController extends BaseController {


    @Autowired
    private UserService userService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "/list",name = "分页展示用户数据列表展示") // page代表当前的页码  page=2
    public String findAll(@RequestParam(value = "page",defaultValue = "1") Integer pageNum , @RequestParam(defaultValue = "10")Integer pageSize){
        PageInfo pageInfo = userService.findPage(getCompanyId(),pageNum,pageSize);
        request.setAttribute("page",pageInfo); //这里的page名称不能改，因为页面要的就是page这个名称
        return "system/user/user-list";
    }

    @RequestMapping(value = "/toAdd",name = "跳转到新增用户页面")
    public String toAdd(){
//        查询本企业的所有部门
        List<Dept> deptList = deptService.findAll(getCompanyId());
        request.setAttribute("deptList",deptList);

        return "system/user/user-add";
    }

    @RequestMapping(value = "/edit",name = "保存用户信息")
    public String edit(User user){

        if(StringUtils.isNotEmpty(user.getId())){
//           id不为空，应该执行修改方法
            userService.update(user);
        }else{
            user.setId(UUID.randomUUID().toString());
            user.setCompanyId(getCompanyId());
            user.setCompanyName(getCompanyName());
            userService.save(user);
        }
        return "redirect:/system/user/list.do"; // 重定向到列表页面
    }
    @RequestMapping(value = "/toUpdate",name = "跳转到修改用户页面")
    public String toUpdate(String id){
        User user = userService.findById(id);
        request.setAttribute("user",user);

//        查询本企业的所有部门
        List<Dept> deptList = deptService.findAll(getCompanyId());
        request.setAttribute("deptList",deptList);

//        新增和修改页面共用一个
        return "system/user/user-add";
    }
    @RequestMapping(value = "/delete",name = "删除用户数据")
    public String delete(String id){
        userService.deleteById(id);
        return "redirect:/system/user/list.do"; // 重定向到列表页面
    }
    @RequestMapping(value = "/deleteByIds",name = "删除用户数据")
    public String deleteByIds(String[] ids){

        for (String id : ids) {
            userService.deleteById(id);
        }

        return "redirect:/system/user/list.do"; // 重定向到列表页面
    }
    @RequestMapping(value = "/roleList",name = "进入到用户分配角色页面")
    public String roleList(String id){ // id  用户id
//        1、所有的角色数据
        List<Role> roleList = roleService.findAll(getCompanyId());
        request.setAttribute("roleList",roleList);
//       2、当前用户对象
        User user = userService.findById(id);
        request.setAttribute("user",user);
//        3、当前用户曾经拥有的角色id 拼接成一个字符串 "1,2,3"  select role_id from pe_role_user  where user_id=?
        List<String> roleIdsList = userService.findRoleIdsByUserId(id);

//        StringBuffer idsStr = new StringBuffer("");
//        for (String roleId : roleIdsList) {
//            idsStr.append(roleId).append(",");
//        }
//        request.setAttribute("userRoleStr",idsStr.toString());

        String idsStr = roleIdsList.stream().collect(Collectors.joining(","));
        request.setAttribute("userRoleStr",idsStr);

        return "system/user/user-role"; //
    }

    @RequestMapping(value = "/changeRole",name = "用户分配角色方法")
    public String changeRole(String userid ,String[] roleIds){

        userService.changeRole(userid,roleIds);

        return "redirect:/system/user/list.do"; // 完成用户分配角色后跳转到用户的列表页面
    }


}
